/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.*;
import org.springblade.modules.repair.service.*;
import org.springblade.modules.repair.vo.BuildingElevatorTemplateVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.repair.vo.BuildingFloorElevatorVO;
import org.springblade.modules.repair.wrapper.BuildingFloorElevatorWrapper;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 控制器
 *
 * @author ysq
 * @since 2024-07-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/buildingfloorelevator")
@Api(value = "", tags = "楼栋管理")
public class BuildingFloorElevatorController extends BladeController {

	private final IBuildingFloorElevatorService buildingFloorElevatorService;
	private final IElevatorService elevatorService;
	private final IBuildingElevatorTemplateService betService;
	private final ElevatorDetailService elevatorDetailService;
	private final IBuildingFloorElevatorNumService buildingFloorElevatorNumService;

	@GetMapping("listOfbuildingId")
	public R<BuildingFloorElevatorVO> listOfbuildingId(Long buildingId) {
		BuildingFloorElevatorVO buildingFloorElevatorVO = buildingFloorElevatorService.listOfbuildingId(buildingId);
		return R.data(buildingFloorElevatorVO);
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入buildingFloorElevator")
	public R<BuildingFloorElevator> detail(Long id) {
		BuildingFloorElevator detail = buildingFloorElevatorService.getById(id);
		List<Elevator> elevators = elevatorService.list(Wrappers.<Elevator>lambdaQuery().eq(Elevator::getFloorElevatorId, id));
		List<Long> ids = elevators.stream().map(Elevator::getId).collect(Collectors.toList());
		detail.setElevatorIds(ids);
		List<BuildingFloorElevatorNum> list = buildingFloorElevatorNumService.list(Wrappers.<BuildingFloorElevatorNum>lambdaQuery().eq(BuildingFloorElevatorNum::getFloorElevatorId, id));
		detail.setNumLists(list);
		return R.data(detail);
	}


	@GetMapping("/building/elevator/template/list")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "根据项目获取所有项目电梯关联模板")
	public R getBuildingElevatorTemplateList(Long buildingId) {
		List<BuildingElevatorTemplate> buildingElevatorTemplates = betService.list(Wrappers.<BuildingElevatorTemplate>lambdaQuery().eq(BuildingElevatorTemplate::getBuildingId, buildingId));
		if (ObjectUtil.isNotEmpty(buildingElevatorTemplates)) {
			buildingElevatorTemplates.forEach(e -> {
				List<String> names = Func.toStrList("/", e.getName());
				e.setTemplates(names.stream().map(k -> {
					BuildingElevatorTemplateVO templateVO = new BuildingElevatorTemplateVO();
					templateVO.setName(k);
					return templateVO;
				}).collect(Collectors.toList()));
			});
		}
		return R.data(buildingElevatorTemplates);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save/or/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入buildingFloorElevator")
	public R<Boolean> save(@Valid @RequestBody BuildingFloorElevator buildingFloorElevator) {
		if (buildingFloorElevator.getElevatorIds() != null && buildingFloorElevator.getElevatorIds().size() > 0) {
			List<Elevator> list = elevatorService.listByIds(buildingFloorElevator.getElevatorIds());
			if (buildingFloorElevator.getId() != null) {
				List<Elevator> collect = list.stream().filter(o -> o.getFloorElevatorId() != null && o.getFloorElevatorId() != -1L && !o.getFloorElevatorId().equals(buildingFloorElevator.getId())).collect(Collectors.toList());
				if (collect.size() > 0) {
					List<ElevatorDetail> list2 = elevatorDetailService.list(Wrappers.<ElevatorDetail>lambdaQuery().in(ElevatorDetail::getElevatorId, collect.stream().map(Elevator::getId).collect(Collectors.toList())));
					String join = String.join(",", list2.stream().map(ElevatorDetail::getElevatorAddress).collect(Collectors.toList()));
					throw new ServiceException(join + "已经绑定，请重新选择电梯");
				}
			} else {
				List<Elevator> collect = list.stream().filter(o -> o.getFloorElevatorId() != null && o.getFloorElevatorId() != -1L).collect(Collectors.toList());
				if (collect.size() > 0) {
					List<ElevatorDetail> list2 = elevatorDetailService.list(Wrappers.<ElevatorDetail>lambdaQuery().in(ElevatorDetail::getElevatorId, collect.stream().map(Elevator::getId).collect(Collectors.toList())));
					String join = String.join(",", list2.stream().map(ElevatorDetail::getElevatorAddress).collect(Collectors.toList()));
					throw new ServiceException(join + "已经绑定，请重新选择电梯");
				}
			}
		}
		buildingFloorElevatorService.saveOrUpdate(buildingFloorElevator);
		List<Elevator> list = elevatorService.list(Wrappers.<Elevator>lambdaQuery().eq(Elevator::getFloorElevatorId, buildingFloorElevator.getId()));
		if (buildingFloorElevator.getElevatorIds() != null && buildingFloorElevator.getElevatorIds().size() > 0) {
			List<Long> elevatorIds = buildingFloorElevator.getElevatorIds();
			List<Elevator> elevators = elevatorService.listByIds(elevatorIds);
			elevators.forEach(o -> o.setFloorElevatorId(buildingFloorElevator.getId()));
			elevatorService.updateBatchById(elevators);
			List<Elevator> elevators1 = list.stream().filter(o -> !buildingFloorElevator.getElevatorIds().contains(o.getId())).collect(Collectors.toList());
			if (elevators1.size() > 0) {
				elevators1.forEach(o -> o.setFloorElevatorId(-1L));
				elevatorService.updateBatchById(elevators1);
			}
		} else {
			list.forEach(o -> o.setFloorElevatorId(-1L));
			elevatorService.updateBatchById(list);
		}
		if (buildingFloorElevator.getNumLists() == null) {
			buildingFloorElevator.setNumLists(new ArrayList<>());
		}
		buildingFloorElevatorNumService.saveList(buildingFloorElevator.getNumLists(), buildingFloorElevator.getId());
		return R.data(true);
	}


	/**
	 * 删除
	 */
	@GetMapping("/remove/elevator")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入id")
	public R<Boolean> removeElevator(Long elevatorId) {
		Elevator elevator = elevatorService.getById(elevatorId);
		elevator.setFloorElevatorId(-1L);
		elevatorService.updateById(elevator);
		return R.data(true);
	}
}
